//
//  ZYStatusPictureView.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/14.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit

class ZYStatusPictureView: UICollectionView {

    var pictureInfos: [ZYPictureInfo]? {
        didSet{
            //
            countLabel.text = "\(pictureInfos?.count ?? 0)"
            let count = pictureInfos?.count ?? 0
            
            let size = getPictureViewSize(count)
            //更新约束
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            //刷新
            reloadData()
            }
        }
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI(){
        backgroundColor = UIColor.clearColor()
        addSubview(countLabel)
        countLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        //注册
        registerClass(ZYStatusPictureViewCell.self, forCellWithReuseIdentifier: pictureCellIdentifier)
        //设置代理（collection的）
        dataSource = self
        
        let flowlayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        flowlayout.itemSize = CGSize(width: cellWH, height: cellWH)
        //设置flowlayout  -垂直、水平间距
        flowlayout.minimumInteritemSpacing = margin
        flowlayout.minimumLineSpacing = margin
    }
    
    private lazy var countLabel: UILabel = UILabel(textColor: AppThemeColor, textSize: 35)
    
}

// 计算item图片尺寸
let margin:CGFloat = 5
let cellWH:CGFloat = 80
extension ZYStatusPictureView {
    
    func getPictureViewSize(count:Int) -> CGSize{
        let col = count==4 ? 2 : count > 3 ? 3 : count
        let row = count==4 ? 2 : (count-1)/3+1
        
        let width = CGFloat(col) * cellWH + CGFloat(col-1) * margin
        let height = CGFloat(row) * cellWH + CGFloat(row-1) * margin
        
        return CGSize(width: width, height: height)
    }
    
    
}

// Picture---collectionView
let pictureCellIdentifier = "pictureCellIdentifier"
extension ZYStatusPictureView: UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureInfos?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellIdentifier, forIndexPath: indexPath) as! ZYStatusPictureViewCell
        cell.picUrl = pictureInfos![indexPath.item]
        return cell
    }
    
}

class ZYStatusPictureViewCell:UICollectionViewCell {
    
    var picUrl:ZYPictureInfo?{
        didSet{
            bgImg.zy_setImageWithString(picUrl?.thumbnail_pic, placeholderImgName: "weibo_placeholder")
            if let pic = picUrl?.thumbnail_pic where pic.hasSuffix(".gif"){
                gifImg.hidden = false
            }else{
                gifImg.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        serupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func serupUI(){
        backgroundColor = UIColor.clearColor()
        contentView.addSubview(bgImg)
        contentView.addSubview(gifImg)
        
        bgImg.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImg.snp_makeConstraints { (make) -> Void in
            make.bottom.trailing.equalTo(bgImg)
        }
    }
    
    //背景图
    private lazy var bgImg: UIImageView = {
        let img = UIImageView()
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()
    //gif小图标
    private lazy var gifImg:UIImageView = UIImageView(imgName: "timeline_image_gif")
}






